package tomasulo.unit;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;

public class Memory {
	
	public static final int DEFAULT_CAPACITY = 4096;
	
	private Vector<Float> mem;
	private Queue<Integer> changeList;
	
	public Memory(int capacity) {
		mem = new Vector<Float>(capacity);
		for (int i = 0; i < capacity; ++i) {
			mem.add(new Float(0));
		}
		changeList = new LinkedList<Integer>();
	}
	
	public float getAt(int index) {
		return mem.get(index);
	}
	
	public void setAt(int index, float value, boolean toBeRefreshed) {
		if (toBeRefreshed) {
			changeList.add(new Integer(index));
		}
		mem.set(index, new Float(value));
	}
	
	public void reset() { 
		for (int i = 0; i < DEFAULT_CAPACITY; ++i) {
			setAt(i, 0.0f, true);
		}
	}
	
	public Iterator<Integer> getChangeListIterator() {
		return changeList.iterator();
	}
	
	public void clearChangeList() {
		changeList.clear();
	}
	
}
